package com.example.yuejianapp14.Activity.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.example.yuejianapp14.Activity.Activity.ReleaseActivity;
import com.example.yuejianapp14.Activity.Adapter.TieziAdapter;
import com.example.yuejianapp14.Activity.JavaBean.Tiezi;
import com.example.yuejianapp14.Activity.Listener.TieziListener;
import com.example.yuejianapp14.Activity.Model.TieziModel;
import com.example.yuejianapp14.R;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class TieziFragment extends Fragment {
    private RecyclerView recyclerView;
    private List mDataList;
    private View view;
    private ImageView release;



    public TieziFragment() {
        // Required empty public constructor
    }
    TieziListener tieziListener = new TieziListener() {
        @Override
        public void onResponse(List<Tiezi> beanList) {
            mDataList=beanList;
            TieziAdapter adapter = new TieziAdapter(getActivity(),mDataList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_tiezi, container, false);
        initRecyclerView();
        getData();
        return view;
    }
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageButton release = (ImageButton) getActivity().findViewById(R.id.release);
        release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ReleaseActivity.class);
                startActivity(intent);
            }
        });
    }
    private void getData() {
        TieziModel tieziModel =new TieziModel();
        tieziModel.getTieziList(tieziListener);
    }

    private void initRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager((this.getActivity())));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}
